package com.verisette.ncos.service.master.bizunit;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bean.UserBean;
import org.corp.agi.service.bizunit.CommonBusinessUnit;
import com.verisette.ncos.persistence.dao.UserGroupAuthorizationDAOImpl;
import com.verisette.ncos.persistence.domain.CustomMasterObjectAuthByUsg;
import com.verisette.ncos.persistence.domain.MasterObject;

import com.verisette.ncos.web.master.bean.UserGroupAuthorizationBean;

/**
 * {Description here}
 *
 * @author Thippawan Muangchu (Tip)
 * @since Apr 5, 2010 9:47:59 AM
 * @version 1.0.0
 * @see 
 */
public class UpdateUserGroupAuthorizationBU  extends CommonBusinessUnit {

	private Logger logger = Logger.getLogger(UpdateUserGroupAuthorizationBU.class);

	//initial DAO
	private UserGroupAuthorizationDAOImpl userGroupAuthDAO;
	@Required
	@Autowired
	public void setUserGroupAuthorizationDAO(UserGroupAuthorizationDAOImpl userGroupAuthDAO) {
		this.userGroupAuthDAO = userGroupAuthDAO;
	}
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		UserGroupAuthorizationBean bean = (UserGroupAuthorizationBean) processContext.getValueObject();
		UserBean user = processContext.getUserBean();
		
		//check isObjList for insert/update
		logger.debug("IsObjListAll ==>" + bean.getIsObjListAll());
		//insert
		if(bean.getIsObjListAll()==true){
			List<String> checked = bean.getChecked();
			List<MasterObject> masterObj = bean.getListMasterObject();
			CustomMasterObjectAuthByUsg customObjAuthByUsg;
			List<CustomMasterObjectAuthByUsg> obj = new ArrayList<CustomMasterObjectAuthByUsg>();
			int maxOagId = userGroupAuthDAO.findMaxOagId();
			//loop
			for(int i=0;i<masterObj.size();i++){
				maxOagId += 1;
				customObjAuthByUsg = new CustomMasterObjectAuthByUsg();
				customObjAuthByUsg.setOagId(maxOagId);
				customObjAuthByUsg.setObjId(masterObj.get(i).getObjId());
				customObjAuthByUsg.setUsgId(String.valueOf(bean.getUsgId()));
				obj.add(customObjAuthByUsg);
			}
			
			boolean foundChecked = false;
			for(int j=0;j<obj.size();j++){
				if((null!=checked)&&(checked.size()!=0)){
					for(int i=0;i<checked.size();i++){
						if(checked.get(i).equals(obj.get(j).getObjId())){
							// User check the authorize
							obj.get(j).setOagEnable("T");
							userGroupAuthDAO.insertUserGroup(obj.get(j), user);
							userGroupAuthDAO.flush();
							foundChecked = true;
							break;
						}
						//}else if(i==(checked.size()-1)){
						/*}else{
							obj.get(j).setOagEnable("F");
							userGroupAuthDAO.insertUserGroup(obj.get(j));
							userGroupAuthDAO.flush();
						}*/
					}
					
					if(!foundChecked){
						// User not check the authorize case
						obj.get(j).setOagEnable("F");
						userGroupAuthDAO.insertUserGroup(obj.get(j), user);
						userGroupAuthDAO.flush();
					}
					
				}else{
					// User not check any authorizes case
					obj.get(j).setOagEnable("F");
					userGroupAuthDAO.insertUserGroup(obj.get(j), user);
					userGroupAuthDAO.flush();
				}
				
				foundChecked = false;
				
			}
			
		//update
		}else{			
			List<String> checked = bean.getChecked();
			List<CustomMasterObjectAuthByUsg> obj = bean.getListCustomMasterObjectAuthByUsg();

			for(int j=0;j<obj.size();j++){
				if((null!=checked)&&(checked.size()!=0)){
					for(int i=0;i<checked.size();i++){
						if(checked.get(i).equals(obj.get(j).getObjId())){
							obj.get(j).setOagEnable("T");
							userGroupAuthDAO.updateUserGroup(obj.get(j), user);
							userGroupAuthDAO.flush();
							break;
						}else if(i==(checked.size()-1)){
							obj.get(j).setOagEnable("F");
							userGroupAuthDAO.updateUserGroup(obj.get(j), user);
							userGroupAuthDAO.flush();
						}
					}
				}else{
					obj.get(j).setOagEnable("F");
					userGroupAuthDAO.updateUserGroup(obj.get(j), user);
					userGroupAuthDAO.flush();
				}
			}
			
		}
	}
	
	//update
	public void update(CustomMasterObjectAuthByUsg data){
		commonDAO.update(data);
		commonDAO.flush();
	}
	
	//insert
	public void insert(MasterObject data){
		commonDAO.save(data);
		commonDAO.flush();
	}

}
